package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.PrivacyInfo;
import com.rongshu.view.userInfo.iview.PushNotificationView;
import com.rongshu.view.userInfo.presenter.PushNotificationPresenter;
import com.widget.SwitchButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PushNotificationActivity extends BaseActivity<PushNotificationPresenter> implements PushNotificationView, SwitchButton.OnCheckedChangeListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.txt_message_notification)
    TextView txt_message_notification;
    @BindView(R.id.sb_praise)
    SwitchButton sb_praise;
    @BindView(R.id.sb_comment)
    SwitchButton sb_comment;
    @BindView(R.id.sb_at)
    SwitchButton sb_at;
    @BindView(R.id.sb_personal_letter)
    SwitchButton sb_personal_letter;
    @BindView(R.id.sb_attention_people_article)
    SwitchButton sb_attention_people_article;

    //是否开启通知
    private boolean areNotificationsEnabled = false;
    //map
    private Map<String, Object> map = null;
    private int push_zan, push_pinglun, push_aite, push_wenzhang, push_send;

    @Override
    protected int initLayout() {
        return R.layout.activity_push_notification_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PushNotificationPresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.setting_push_notification);
    }

    @Override
    protected void initData() {
        //listener
        sb_praise.setOnCheckedChangeListener(this);
        sb_comment.setOnCheckedChangeListener(this);
        sb_at.setOnCheckedChangeListener(this);
        sb_personal_letter.setOnCheckedChangeListener(this);
        sb_attention_people_article.setOnCheckedChangeListener(this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getPrivacy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
        //消息推送
        txt_message_notification.setTextColor(areNotificationsEnabled ?
                getResources().getColor(R.color.color_3cc051) : getResources().getColor(R.color.color_9f9f9f));
        txt_message_notification.setText(areNotificationsEnabled ? "已开启" : "已关闭");
    }

    /**
     * 更新view
     */
    private void refreshView(PrivacyInfo.ResponseBean responseBean) {
        push_zan = responseBean.getPush_zan();
        push_pinglun = responseBean.getPush_pinglun();
        push_aite = responseBean.getPush_aite();
        push_send = responseBean.getPush_send();
        push_wenzhang = responseBean.getPush_wenzhang();
        sb_praise.setChecked(push_zan == 1 ? true : false);
        sb_comment.setChecked(push_pinglun == 1 ? true : false);
        sb_at.setChecked(push_aite == 1 ? true : false);
        sb_personal_letter.setChecked(push_send == 1 ? true : false);
        sb_attention_people_article.setChecked(push_wenzhang == 1 ? true : false);
    }

    /**
     * 获取隐私设置/消息推送设置
     */
    private void getPrivacy() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getPrivacy(user_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 消息推送设置
     */
    private void setMsgPush() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("push_zan", push_zan);
        map.put("push_pinglun", push_pinglun);
        map.put("push_aite", push_aite);
        map.put("push_wenzhang", push_wenzhang);
        map.put("push_send", push_send);
        presenter.setMsgPush(user_id,
                (int) map.get("push_zan"),
                (int) map.get("push_pinglun"),
                (int) map.get("push_aite"),
                (int) map.get("push_wenzhang"),
                (int) map.get("push_send"),
                Common.encryptionSign(map));
        map.clear();
    }

    @OnClick({R.id.layout_back, R.id.layout_notification})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.layout_notification://消息推送
                settingNotification();
                break;
        }
    }

    /**
     * 通知设置
     */
    private void settingNotification() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(localIntent);
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        switch (view.getId()) {
            case R.id.sb_praise:
                push_zan = isChecked ? 1 : 0;
                break;
            case R.id.sb_comment:
                push_pinglun = isChecked ? 1 : 0;
                break;
            case R.id.sb_at:
                push_aite = isChecked ? 1 : 0;
                break;
            case R.id.sb_personal_letter:
                push_send = isChecked ? 1 : 0;
                break;
            case R.id.sb_attention_people_article:
                push_wenzhang = isChecked ? 1 : 0;
                break;
        }
        setMsgPush();
    }

    /**
     * 隐私设置/消息推送信息回调
     *
     * @param info
     */
    @Override
    public void onPrivacyInfo(PrivacyInfo info) {
        if (info.getErrCode() == 0) {
            refreshView(info.getResponse());
        } else {
            toast(info.getErrMsg());
        }
    }

    /**
     * 消息推送设置回调
     */
    @Override
    public void onMsgPush(INetBean bean) {

    }
}
